import 'package:flutter/material.dart';
import 'dart:math' as math;

class LoadingView extends StatefulWidget {
  @override
  _LoadingViewState createState() => _LoadingViewState();
}

class _LoadingViewState extends State<LoadingView> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 3), // 动画周期
      vsync: this,
    )..repeat(); // 循环动画
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _controller,
      builder: (BuildContext context, Widget? child) {
        return CustomPaint(
         // size: Size(200, 200),
          painter: CircleRingPainter(animationValue: _controller.value),
        );
      },
    );
  }
}

class CircleRingPainter extends CustomPainter {
  final double animationValue;
  final Paint whiteCirclePaint;
  final Paint blueArcPaint;

  CircleRingPainter({required this.animationValue})
      : whiteCirclePaint = Paint()
    ..color = Colors.white
    ..style = PaintingStyle.stroke
    ..strokeWidth = 4.0,
        blueArcPaint = Paint()
          ..color = Colors.blue
          ..style = PaintingStyle.stroke
          ..strokeWidth = 4.0;

  @override
  void paint(Canvas canvas, Size size) {
    final center = Offset(size.width / 2, size.height / 2);
    final radius = math.min(size.width / 2, size.height / 2); // 圆环半径减去边宽

    // 绘制白色圆环
    canvas.drawCircle(center, radius, whiteCirclePaint);

    // 计算蓝色1/4圆环的位置
    final startAngle = -math.pi * 2/ 5 + (animationValue * 2 * math.pi); // 起始角度
    final sweepAngle = math.pi * 2/ 5; // 扫过的角度（1/4圆）

    // 绘制蓝色1/4圆环
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      startAngle,
      sweepAngle,
      false,
      blueArcPaint,
    );
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}